<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<script type="text/javascript" charset="utf-8">
	var admin_unit_datagrid;
	$(function(){
		var editRow = undefined;
		
		admin_unit_searchForm=$('#admin_unit_searchForm').form();
		
		admin_unit_datagrid=$('#admin_unit_datagrid').datagrid({
			url:'${pageContext.request.contextPath}/tunitAction!getDataGrid.action',
			fit : true,
			fitColumns : true,
			border : false,
			pagination:true,
			rownumbers: true,//条数
			
			pagePosition:'bottom',
			pageSize:10,
			pageList:[5,10,15,20,25],
			sortName:'unitName',
			sortOrder:'desc	',
			idField:'id',
			frozenColumns:[[
				{
					title:'编号',
					field:'id',
					width:100,
					checkbox : true,
				},
				{
					title:'网点名称',
					field:'unitName',
					width:100,
					sortable:'true',
					editor:{
						type:'validatebox',
						options:{
							required:true
						}
					}
				}
			]],
			columns:[[			
				{
					title:'所属小组',
					field:'unitGroupName',
					width:100,
					editor:{
						type:'combobox',
						options:{
							url : 'tunitGroupAction!getTunitGroupList.action',
							valueField : 'unitGroupName',
							textField : 'unitGroupName',
							required:true
						}
					}
				}
			]],
			toolbar:[
				{
					text:'增加',
					iconCls:'icon-add',
					handler:function(){
					//	changeEditorAddRow();
						 if(editRow!=undefined){
							admin_unit_datagrid.datagrid('endEdit',editRow);
						}
						if(editRow==undefined){
							admin_unit_datagrid.datagrid('insertRow',{
								index: 0,	// �����0��ʼ
								row: {
								}
								
							});
							admin_unit_datagrid.datagrid('beginEdit',0);
							editRow=0;
							//var rows=admin_unit_datagrid.datagrid('getRows');
						}
					}
				},'-',{
					text:'删除',
					iconCls:'icon-remove',
					handler:function(){
						var rows=admin_unit_datagrid.datagrid('getSelections');
						console.info(rows.length);
						if(rows.length>0){
							$.messager.confirm('请确认','确定要删除已选择的所有行吗？',function(b){
								if(b){
									var ids=[];
									/* var index=[]; */
									for ( var i = 0; i < rows.length; i++) {
										ids.push(rows[i].id);
										/* index.push(admin_unit_datagrid.datagrid('getRowIndex',rows[i]));
										console.info(index[i]); */
									}
									$.ajax({
										url:'${pageContext.request.contextPath}/tunitAction!delete.action',
										data:{
											ids:ids.join(','),
										},
										dataType:'json',
										success:function(j){
											if(j&&j.success){
												/* for(var i =0;i<index.length;i++){
													admin_unit_datagrid.datagrid('deleteRow',0);
												} */
												
												$.messager.show({
													title:'提示',
													msg:j.msg,
												});
												admin_unit_datagrid.datagrid('load');
												admin_unit_datagrid.datagrid('unselectAll');
											}else{
												$.messager.alert('提示',j.msg,'warning');
											}
										}
									});
								}
							});
						
						}else{
							$.messager.alert('提示','请选择要删除的记录','warning');
						}
					}
				},'-',{
					text:'修改',
					iconCls:'icon-edit',
					handler:function(){
						//changeEditorEditRow();
						var rows=admin_unit_datagrid.datagrid('getSelections');
						if(rows.length==1){
							if(editRow!=undefined){
								admin_unit_datagrid.datagrid('endEdit',editRow);
							}
							if(editRow==undefined){
								var index=admin_unit_datagrid.datagrid('getRowIndex',rows[0]);
								admin_unit_datagrid.datagrid('beginEdit',index);
								admin_unit_datagrid.datagrid('unselectAll');
								editRow=index;
							}
						}
					}
				},'-',{
					text:'保存',
					iconCls:'icon-save',
					handler:function(){
						admin_unit_datagrid.datagrid('endEdit',editRow);
					}
				},'-',{
					text:'取消编辑',
					iconCls:'icon-undo',
					handler:function(){
						editRow=undefined;
						admin_unit_datagrid.datagrid('rejectChanges');
						admin_unit_datagrid.datagrid('unselectAll');
					}
				},'-',{
					text:'全选',
					iconCls:'icon-redo',
					handler:function(){
						editRow=undefined;
						admin_unit_datagrid.datagrid('selectAll');
					}
				},],
				onAfterEdit:function(rowIndex,rowData,changes){
					console.info(rowData);
					editRow=undefined;
					var insert=admin_unit_datagrid.datagrid('getChanges','inserted');
					console.info(insert);
					var update=admin_unit_datagrid.datagrid('getChanges','updated');
					console.info(update);
					var url='';
					if(insert.length>0){
						url='${pageContext.request.contextPath}/tunitAction!addUnit.action';
					}
					if(update.length>0){
						url='${pageContext.request.contextPath}/tunitAction!update.action';
					}
					$.ajax({
							url:url,
							type:'POST',
							data:rowData,
							dataType:'json',
							success:function(j){
								if(j&&j.success){
									admin_unit_datagrid.datagrid('acceptChanges');
									admin_unit_datagrid.datagrid('load');
									$.messager.show({
										msg:j.msg,
										title:'提示'
									});
								}else{
									admin_unit_datagrid.datagrid('rejectChanges');
									$.messager.show({
										msg:j.msg
									});
								}
							}
						});
				},
				onDblClickRow:function(rowIndex,rowData){
				//	changeEditorEditRow();
					if(editRow!=undefined){
						admin_unit_datagrid.datagrid('endEdit',editRow);
					}
					if(editRow==undefined){
						admin_unit_datagrid.datagrid('beginEdit',rowIndex);
						editRow=rowIndex;
					}
				}
		});
		
		checkUser1=function(){
			admin_unit_datagrid.datagrid('load',serializeObject(admin_unit_searchForm));
		};
		clean1=function(){
			admin_unit_datagrid.datagrid('load',{});
			admin_unit_searchForm.find('input').val('');
		};
		changeEditorAddRow=function(){
			admin_unit_datagrid.datagrid('removeEditor','createTime');
		};
		changeEditorEditRow=function(){
			admin_unit_datagrid.datagrid('removeEditor',['createTime']);
		};
	});
</script>
<div class="easyui-layout" data-options="fit:true,border:false" >
	<div region="north" border="false" title="条件查询" style="height: 60px;overflow: hidden;">
		<form id="admin_unit_searchForm">
			<table  class="datagrid-toolbar">
				<tr>
					<th align="right">网点名称</th>
					<td align="left">
					<input name="unitName"/>
					<a href="javascript:void(0);" class="easyui-linkbutton" onclick="checkUser1()">查询</a>
					<a href="javascript:void(0);" class="easyui-linkbutton" onclick="clean1()">清空</a>
					</td>
				</tr>
			</table>
		</form>
	</div>
	<div data-options="region:'center'">
		<table id="admin_unit_datagrid">
		</table>
	</div>
</div>
